package com.wanho.java159.cb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanho.java159.cb.mapper.SkillMapper;
import com.wanho.java159.cb.po.Skill;
import com.wanho.java159.cb.po.SkillExample;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
* Skill 业务处理类
* @author ChaoFei.Zhang
* @date   2021-07-10
*/
@Service
@Transactional
public class SkillService {
    /**Skill数据访问层对象*/
    @Resource
    private SkillMapper skillMapper ;

    /**
    * 查询所有 Skill 状态为未删除 is_deleted=0 的数据
    * @return Skill所有的数据
    */
    public List<Skill> list(){
        SkillExample skillExample = new SkillExample();
        //查询 数据表中 is_deleted = 0
        skillExample.createCriteria().andIsDeletedEqualTo(false) ;
        return skillMapper.selectByExample(skillExample) ;
    }

    /**
    * 根据删除主键删除 Skill 逻辑删除 进行is_deleted=1的修改操作
    * @param id Skill的主键
    * @return true 删除成功  false 删除失败
    */
    public boolean remove(Long id){
        Skill skill = new Skill();
        skill.setId(id);
        skill.setIsDeleted(true);
        return skillMapper.updateByPrimaryKeySelective(skill) > 0 ;
    }

    /**
    * 新增一条Skill记录
    * @param  skill 封装新增的数据
    * @return true 新增成功  false 新增失败
    */
    public boolean save(Skill skill){
        skill.setCreateTime(new Date());
        skill.setUpdateTime(new Date());
        skill.setIsDeleted(false);
        return skillMapper.insert(skill)>0 ;
    }

    /**
    * 根据主键查询 Skill
    * @param id Skill的主键
    * @return null id不存在  返回Skill对象
    */
    public Skill get(Long id){
        return skillMapper.selectByPrimaryKey(id) ;
    }

    /**
    * 修改一条Skill记录
    * @param  skill 封装修改的数据
    * @return true 修改成功  false 修改失败
    */
    public boolean update(Skill skill) {
        skill.setUpdateTime(new Date());
        return skillMapper.updateByPrimaryKeySelective(skill)>0 ;
    }

    /**
    * 分页查询 Skill  如果有查询条件需要动态在方法体内添加条件
    * @param pageNum 页码
    * @param pageSize 分页跨度
    * @param skill 封装查询的数据
    * @return 分页对象
    */
    public PageInfo<Skill> listPage(Long pageNum, Long pageSize,Skill skill) {
        PageHelper.startPage(pageNum.intValue(),pageSize.intValue());
        SkillExample skillExample = new SkillExample();
        SkillExample.Criteria criteria = skillExample.createCriteria();
        return new PageInfo<>(skillMapper.selectByExample(skillExample));
    }
}